package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.po.DtNewsSort;
import com.xueyi.aixihu.entity.vo.ComplainRecordVo;
import com.xueyi.aixihu.reqeust.ComplaintReq;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Mapper
public interface ComplaintMapper {

    /**
     * 条件查询投诉信息
     */
    @Select({
            " <script> " ,
            "   select  ",
            "   t1.*,",
            "   CONCAT(if(t1.nike_name='' or t1.nike_name is null,'匿名',t1.nike_name),'-',if(t1.user_phone='' or t1.user_phone is null,'未填写',t1.user_phone)) contactInfo, ",
            "   t2.name addressName from  ",
            "( ",
                "select * from cdz_complain_record t1" +
                " <where>" +
                    " <if test='currentStatus != null '> " +
                    "  and current_status = #{currentStatus} " +
                    " </if>" +
                    " <if test='areaId != null '> " +
                    "  and address_id = #{areaId} " +
                    " </if>" +
                    " <if test='keyWord != null '> " +
                    " and " +
                    " (" +
                        "nike_name like concat('%',#{keyWord},'%') or " +
                        "user_Phone like concat('%',#{keyWord},'%') or " +
                        "complain_title like concat('%',#{keyWord},'%') or " +
                        "remark like concat('%',#{keyWord},'%') " +
                    " ) " +
                    " </if>" +
                " </where>" +
            " ) t1 left join dt_news_sort t2 on t1.address_id=t2.id ",
            " order by t1.create_time asc ",
            " limit #{startIndex},#{limit} ",
            "</script>"
    })
    List<ComplainRecordVo> list(@Param("currentStatus") Integer currentStatus,
                                @Param("keyWord") String keyWord,
                                @Param("areaId") Integer areaId,
                                @Param("startIndex") Integer startIndex,
                                @Param("limit") Integer limit);

    @Select({
        "<script>",
            "select  ",
            "IFNULL(sum(if(current_status=0,1,0)),0) waitAuditNumber , ",
            "IFNULL(sum(if(current_status=1,1,0)),0) auditPassedNumber  ",
            "from cdz_complain_record ",
            " <where>" ,
                " <if test='areaId != null '> " ,
                "  and address_id = #{areaId} " ,
                " </if>" ,
                " <if test='keyWord != null '> " ,
                " and " +
                    " (" +
                        "nike_name like concat('%',#{keyWord},'%') or " ,
                        "user_Phone like concat('%',#{keyWord},'%') or " ,
                        "complain_title like concat('%',#{keyWord},'%') or " ,
                        "remark like concat('%',#{keyWord},'%') " ,
                    " ) " ,
                " </if>" ,
            " </where>" ,
        "</script>"
    })
    Map<String,Integer> statisticsTotalNumber(@Param("keyWord") String keyWord,
                                              @Param("areaId") Integer areaId);

    @Select({
        "<script> ",
            " select * from dt_news_sort where pid!=2 and is_valid=1 ",
        "</script> ",
    })
    List<DtNewsSort> selectAllAreaInfoList();

    /**
     * 处理投诉信息
     */
    @Update({"<script>" +
            " UPDATE cdz_complain_record " +
            " <set>" +
            " <if test='currentStatus != null '> " +
            " current_status = #{currentStatus}, " +
            " </if>" +
            " <if test='date != null '> " +
            " process_time = #{date} ," +
            " </if>" +
            " <if test='processRemark != null '> " +
            " process_remark = #{processRemark} " +
            " </if>" +
            " </set>" +
            " where id=#{id}" +
            " </script>"})
    void deal(@Param("id") Integer id, @Param("currentStatus") Integer currentStatus, @Param("date") Date date, @Param("processRemark") String processRemark);

    @Insert({
            "<script>" ,
            "insert into cdz_complain_record " ,
            "<trim prefix='(' suffix=')' suffixOverrides=','>",
                "<if test='nikeName != null'>",
                "   nike_name,",
                "</if>",
                "<if test='userPhone != null'>",
                "   user_phone,",
                "</if>",
                "<if test='complatinTitle != null'>",
                "   complain_title,",
                "</if>",
                "<if test='addressId != null'>",
                "   address_id,",
                "</if>",
                "<if test='remark != null'>",
                "   remark,",
                "</if>",
                "<if test='remarkPhoto != null'>",
                "   remark_photo",
                "</if>",
            "</trim>" ,
            "values " ,
            "<trim prefix='(' suffix=')' suffixOverrides=','>",
                "<if test='nikeName != null'>",
                "   #{nikeName},",
                "</if>",
                "<if test='userPhone != null'>",
                "   #{userPhone},",
                "</if>",
                "<if test='complatinTitle != null'>",
                "   #{complatinTitle},",
                "</if>",
                "<if test='addressId != null'>",
                "   #{addressId},",
                "</if>",
                "<if test='remark != null'>",
                "   #{remark},",
                "</if>",
                "<if test='remarkPhoto != null'>",
                "   #{remarkPhoto}",
                "</if>",
            "</trim>" ,
            "</script>"
    })
    void add(@Param("nikeName") String nikeName, @Param("userPhone") String userPhone, @Param("complatinTitle") String complatinTitle,
             @Param("addressId") Integer addressId, @Param("remark") String remark, @Param("remarkPhoto") String remarkPhoto);
}
